package repository

import (
	"gitee.com/anxu/golang-web/src/models"
)

var User = new(user)

type user struct{}

func (u *user) FindByEmail(email string) *models.User {
	user := &models.User{}
	if res := models.Db.Where("email = ?", email).First(user); res.RowsAffected > 0 {
		return user
	}
	return nil
}

func (u *user) FindById(id int) *models.User {
	user := &models.User{}
	if res := models.Db.First(user, id); res.RowsAffected > 0 {
		return user
	}
	return nil
}

func (u *user) Create(user *models.User) *models.User {
	res := models.Db.Create(user)
	if res.RowsAffected > 0 {
		return user
	}
	return nil
}
